<template>
  <div class="app-container">
    <el-form @submit.native.prevent :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="单号" prop="pzbh">
        <el-input
          v-model="queryParams.pzbh"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="enterClick"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="shzt">
        <el-select
            v-model="queryParams.shzt"
            placeholder="请选择"
            clearable
            style="width: 240px"
            @change="changeWarehouseAuditState"
        >
            <el-option
                v-for="item in auditState"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
        </el-select>
      </el-form-item>
        <!-- <el-form-item label="条码" prop="sptm">
        <el-input
          v-model="queryParams.sptm"
          placeholder="请输入条码"
          clearable
        />
      </el-form-item> -->
      <el-form-item label="客户" prop="spkhmc">
        <el-select
          v-model="queryParams.spkhmc"
          placeholder="请选择客户"
          clearable
          filterable
          :loading="selectClient3Loading"
        >
          <el-option
              v-for="(item, index) in selectClient3List"
              :key="index"
              :label="item.khmc"
              :value="item.khmc"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="柜号/批号" prop="spgh">
        <el-input
          v-model="queryParams.spgh"
          placeholder="请输入柜号/批号"
          clearable
          @keyup.enter.native="enterClick"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="spckmc">
        <el-input
          v-model="queryParams.spckmc"
          placeholder="请输入仓库"
          clearable
          @keyup.enter.native="enterClick"
        />
      </el-form-item>
      <el-form-item label="仓位" prop="ckcw">
         <el-input
          v-model="queryParams.ckcw"
          placeholder="请输入仓库"
          clearable
          @keyup.enter.native="enterClick"
        />
      </el-form-item>
      <el-form-item label="调仓日期" prop="warehouseMoveDate">
        <el-date-picker
          v-model="warehouseMoveDate"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          @change="warehouseMoveDateChange()"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item >
        <el-checkbox v-model="queryParams.jqcz" label="精确查找" true-label="1" @change="getList"></el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleWarehouseInnerMove" v-hasPermi="['zhcloud:hwtc:add']">新增库内调仓</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button  type="primary" plain size="mini" @click="handelPatchAudit"  :disabled="selection.length === 0" v-hasPermi="['zhcloud:hwtc:audit']">批量审核</el-button>
      </el-col>
        <el-col :span="1.5">
        <el-button  type="primary" plain size="mini" @click="handleExport"  :disabled="selection.length === 0" v-hasPermi="['zhcloud:hwtc:export']">导出</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="warehouseMoveList"
      @selection-change="handleSelectionChange"
      ref="warehouseMoveTable"
      :show-summary="false"
      :default-sort="{prop: 'prrq', order: 'descending'}"
    >
      <af-table-column type="selection" width="50" align="center" />  
      <af-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100" fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleOpenDetail(scope.row)"
            v-hasPermi="['zhcloud:hwtc:detail']"
          >详细</el-button>
        </template>
      </af-table-column>
      <af-table-column prop="pzbh" label="单据编号" width="100" fixed="left"></af-table-column>
      <af-table-column prop="dzopmc" label="单据类型" ></af-table-column>
      <af-table-column prop="khmc" label="客户" ></af-table-column>
      <af-table-column prop="spjs" label="调入件数" ></af-table-column>
      <af-table-column prop="spsl" label="调入重量(吨)" ></af-table-column>
      <af-table-column prop="prrq" label="创建日期" ></af-table-column>
      <af-table-column prop="rsopmc" label="制单人" ></af-table-column>
      <af-table-column prop="shrsmc" label="审核人" ></af-table-column>
      <af-table-column prop="shrqid" label="审核日期" ></af-table-column>
      <af-table-column prop="shzt" label="审核状态" ></af-table-column>
      <af-table-column prop="dzbz" label="备注"  width="150" :show-overflow-tooltip="true"></af-table-column>
    </el-table>
     <pagination
        v-show="total>0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="getList"
      />
  </div>

</template>

<script>
import { listWarehouseMove, warehouseMoveAudit } from "@/api/storage/commonGoods/warehouseMove";
import { selectClient3 } from "@/api/storage/customer";
import ExportJsonExcel from 'js-export-excel'

export default {
  name: "WarehouseMove",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      //列表加载
      loading: true,
      //客户加载
      selectClient3Loading: true,
      // 表单校验
      rules: {
      },
      //审核状态
      auditState:[{
        value: "未审核",
        label:"未审核"
      },{
        value: "已审核",
        label:"已审核"
      }],
      warehouseMoveDate:'',
      // 查询参数
      queryParams: {
        ckcw: "",
        jqcz: "",
        pzbh: "",
        rqid1: "",
        rqid2: "",
        shzt: "",
        spckmc: "",
        spgh: "",
        spkhmc: "",
        spkhmc2: "",
        sptm: ""
      },
      //获取仓位参数
      selectPositionQuery:{
         spckid:""
      },
      //获取客户参数
      selectCustomerQuery: {
        khmc1: "",
      },
      //批量审核数据
      selection:[],
      // 货物出仓列表
      warehouseMoveList: [],
      //客户列表
      selectClient3List: [],
      //页数
      pageSize: 10,
      //页码
      pageNum:1,
      // 总条数
      total: 0,
    };
  },
  beforeRouteEnter(to, from, next) {
    const params = to.params
    next(vm => {   
      if(Object.keys(params).length > 0) {
        if(to.params.refresh == true) {
          vm.getList()
        }
      }
    })
  },
  created() {
    this.getCustomer()
    this.getList()
  },
  methods: {
    /**获取客户列表 */
    getCustomer() {
      this.selectClient3Loading = true
      selectClient3(this.selectCustomerQuery).then(response => {
        this.selectClient3List = response.data
        this.selectClient3Loading = false
      });
    },
    /** 查询货物调仓列表 */
    getList() {
      this.loading = true
      listWarehouseMove(this.queryParams, {pageNum:this.pageNum, pageSize:this.pageSize}).then(response => {
        this.warehouseMoveList = response.rows;
        this.total = response.total;
        this.loading = false
      });
    },
    /**选择审核状态 */
    changeWarehouseAuditState() {
      this.getList()
    },

    /**选择调仓时间 */
    warehouseMoveDateChange() {
      this.queryParams.rqid1 = this.warehouseMoveDate?this.warehouseMoveDate[0]:""
      this.queryParams.rqid2 = this.warehouseMoveDate?this.warehouseMoveDate[1]:""
      this.getList()
    },

    /**出仓批量审核 */
    auditWarehouseMove() {
      const dzids = []
      const selection = this.selection
      for(let i in selection) {
        dzids.push(selection[i].dzid)
      }
      this.$modal.loading("数据正在审核中，请稍等...")
      warehouseMoveAudit(dzids).then(response => {
        this.$modal.closeLoading()
        this.$modal.msgSuccess('审核成功！')
        this.getList()
      }).catch(()=> {
        this.$modal.closeLoading()
      })
    },

    //详情
    handleOpenDetail(row) {
      const data = JSON.parse(JSON.stringify(row))
      this.$router.push({path:"/storage/commonGoods/warehouseMove/warehouseMoveDetail", query: data})
    },

    /** 增加库内调仓*/
    handleWarehouseInnerMove() {
      this.$router.push({name:"WarehouseInnerMove"})
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1
      this.getList();
    },

    /** 批量审核*/
    handelPatchAudit() {
      const selection = this.selection
      for(let i in selection) {
        if(selection[i].shzt == "已审核") return this.$modal.msgWarning("已审核的单不允许再次审核！")
      }
      this.$modal.confirm('是否确认次操作？').then(() => {
        this.auditWarehouseMove()
      }).catch(() => {});
    },

    /** 导出勾选数据*/
    handleExport() {
      if (this.selection.length > 0) {
        let option = {}
        option.fileName = '货物调仓勾选数据'
        option.datas = [
          {
            sheetData: this.selection,//数据
            sheetName: 'sheet',
            //表头数据对应的sheetData数据
            sheetFilter: ['pzbh', 'dzopmc', 'khmc', 'spjs', 'spsl', 'prrq', 'rsopmc', 'shrsmc', 'shrqid', 'shzt', 'dzbz'],
            sheetHeader: [
              //表头数据
              '单据编号', '单据类型', '客户', '调入件数', '调入重量(吨)', '创建日期','制单人', '审核人','审核日期', '审核状态','备注'],
          }
        ]
        let excel = ExportJsonExcel(option)
        excel.saveExcel()
      } else {
        this.$modal.msgError('请勾选数据');
      }
    },
     // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection
    },
       /**enter键触发 */
    enterClick(e) {
      this.getList()
    },

    /** 刷新数据*/
    refresh() {
      this.getList()
    },

     /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      this.getCustomer()
      this.getList()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
    },
  }
};
</script>
